package sg.edu.nus.iss.billing.parse;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Sammil
 * 
 */

public class Util {

	/**
	 * @param time
	 *            Eg: 00:02:10
	 * @return time in seconds (Returns 170)
	 */

	static DateFormat datetimeformat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
	static DateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");

	public static long getSeconds(String time) {

		long timeInSeconds = 0;
		if (time != null) {
			String timeArray[] = time.split(":");
			if (timeArray.length == 3) {
				timeInSeconds = Integer.parseInt(timeArray[0]) * 60 * 60;
				timeInSeconds += Integer.parseInt(timeArray[1]) * 60;
				timeInSeconds += Integer.parseInt(timeArray[2]);
			} else if (timeArray.length == 2) {
				timeInSeconds = Integer.parseInt(timeArray[0]) * 60;
				timeInSeconds += Integer.parseInt(timeArray[1]);
			} else if (timeArray.length == 1) {
				timeInSeconds = Integer.parseInt(timeArray[0]);

			}
		}
		return timeInSeconds;
	}

	public static Date toDateTime(String dateSting) {

		Date date = null;
		try {
			date = datetimeformat.parse(dateSting);
		} catch (ParseException e) {
			e.printStackTrace();
			
		}
		return date;
	}
	
	public static Date toDate(String dateSting) {

		Date date = null;
		try {
			date = dateformat.parse(dateSting);
		} catch (ParseException e) {
			e.printStackTrace();
			
		}
		return date;
	}

	public static String toString(Date date) {

		String dateSting = null;
		dateSting = dateformat.format(date);
		System.out.println("Date = " + dateSting);
		return dateSting;
	}

	public static void main(String s[]) throws ParseException {

		System.out.println("Seconds " + getSeconds("00:05:10"));
		System.out.println("Seconds " + toDate("10-02-2011 12:35"));
		System.out.println("BD " + new BigDecimal("133.20"));

	}

}
